<template>
  <div>
      <div class="goods-list-back">
         <div @click="bgoBack">
            <van-icon name="arrow-left" color='#fff' size="20px"/>
          </div>
      </div>
      <div class="goods-list" ref="goodList">
        <div class="content">
          <div class="goods-item" v-for="(item,index) in goodslist" :key="index">
            <router-link :to="{path:'/goodsinfo/',query:{goods_id:item.goods_id}}" class="goods-item-center">
              <img :src="item.goods_small_logo" alt="" onerror="javascript:this.src='../../assets/images/image-sb.png';this.onerror = null">
              <div class="item-right">
                  <h1 class="title">{{item.goods_name}}</h1>
                  <p class="info">
                    <span class="price">￥{{item.goods_price}}</span>
                    <span class="sell">剩余{{item.goods_number}}</span>
                  </p>
              </div>
            </router-link>
          </div>
          <div class="goods-bottom" v-if="!isSearch">到底了~</div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { getSearch } from '@/network/search.js'
export default {
  name: 'GoodsList',
  data () {
    return {
      goodslist: [],
      last_id: 0,
      category_id: 0,
      pagenum: 1,
      isSearch: true,
      isloading: false
    }
  },
  created () {
    this.getSearch()
  },
  mounted () {
    this.getBscoll()
  },
  methods: {
    getBscoll () {
      this.$nextTick(() => {
        this.scroll = new BScroll(this.$refs.goodList, {
          /**
       * 是否派发滚动到底部的事件，用于上拉加载
       */
          pullUpLoad: {
            type: Boolean,
            default: true
          },
          /**
       * 点击列表是否派发click事件
       */
          click: {
            type: Boolean,
            default: true
          }
        })
        this.scroll.on('pullingUp', () => {
          if (this.isSearch) {
            if (this.isloading) return
            console.log(this.pagenum + 'xxx')
            this.pagenum += 1
            this.getSearch()
          } else {
            console.log('到底了')
          }
          this.scroll.finishPullUp()
        })
      })
    },
    getSearch () {
      this.isloading = true
      this.category_id = this.$route.params.category_id
      getSearch(this.category_id, this.pagenum).then(res => {
        if (res.message.goods.length === 0) {
          this.isSearch = false
        }
        console.log(res.message.goods.length)
        this.goodslist.push(...res.message.goods)
        this.goodslist = this.goodslist.filter(item => item.goods_price !== 0)
        // console.log(this.goodslist)
        this.isloading = false
      })
    },
    bgoBack () {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
  .goods-list-back{
    position: fixed;
    top: 1rem;
    left: 1rem;
    height: 40px;
    width: 40px;
    background-color: rgba(0, 0, 0,.4);
    border-radius: 50%;
    z-index: 999;
    line-height: 40px;
    text-align: center;
  }
  .goods-list{
    overflow: hidden;
    position: absolute;
    top: 0px;
    bottom: 50px;
    .goods-item{
      background-color: #fff;
      display: flex;
      // border: .01rem solid rgb(247, 247, 247);
      padding: 10px;
      .goods-item-center{
        display: flex;
      }
      img{
        width: 30%;
      }
      .item-right{
        padding: 10px 15px;
        flex: 1;
        .title{
          font-size: 16px;
          color: #333;
          margin: 10px 0;
        }
        .info{
          display: flex;
          justify-content: space-between;
          margin-bottom: 0;
          .price{
            color: red;
            font-weight: bold;
            font-size: 15px;
          }
          .sell{
            font-size: 13px;
          }
        }
      }

    }
    .goods-bottom{
      text-align: center;
      border-top: 1px solid #ccc;
      border-bottom: 1px solid #ccc;
      line-height: 40px;
    }
  }

</style>
